var util = require('../../utils/util.js')
var app = getApp();
Page({
  data: {
  txt:{
  },
  txt2: {
  },
  txt3: {
  },
  txt4: {
  },
    dialog4: {
      hidden: true
    },
    dialog5: {
      hidden: true
    },
    name: "小区位置",
    ysex: ['男', '女'],
    ysexinit: 0,
    yowner: ['业主', '家庭', '亲戚', '朋友', '其他'],
    yownerinit: 0,
    yaddress: ['小区名称', '逸景蓝湾', '花样年花都', '花样年花乡', '翔名苑', '云海天城世家', '东华明珠园', '月亮湾山庄',],
    yaddressinit: 0,
    yfilename_num: [],
    yaddresss: '',
    ydetailed: '',
    yname: "",
    yidcar:"",
    yphones: "",
    yindex: "0",
    createtime: "",
    updatetime: "",
    dialog2: { hidden: true },
    dialog3: { hidden: true },
    ynameHidden: { hidden: true },
    yphoneHidden: { hidden: true },
    yfilenameHidden: { hidden: true },
    ydetailedHidden: { hidden: true },
    yfangjianhao:{hidden:true},
    yaddressHidden: { hidden: true },
    imageHidden: { hidden: true },
    systemHidden: { hidden: true },
    longlatitudeHidden: { hidden: true },
    yfangjianhao2: { hidden: true },
    upload: { hidden: true },
    ynumber: "",
    formSubmit: 'submit',
    yfilename: "",

     
  },
  

  //自动加载，获取传递参数
  onLoad: function (opention) {
  //页面传值过来进行处理
    if (opention.phoneto == undefined){//根据选择小区后，回显电话号码
      var phoneto = wx.getStorageSync("phonetwo");
     this.setData({
       txt: phoneto,
     })
     var yidcar = wx.getStorageSync("yidcar");
     this.setData({
       txt2: yidcar,
     })

     var yfanghao = wx.getStorageSync("yfanghao");
     this.setData({
       txt3: yfanghao,
     })

     var ydanyuanlou = wx.getStorageSync("ydanyuanlou");
     this.setData({
       txt4: ydanyuanlou,
     })

   }else{
    this.setData({
      txt: opention.phoneto,
    })
    var yidcar="";
    this.setData({
      txt2: yidcar,
    })

    var yfanghao2 = "";
    this.setData({
      txt3: yfanghao2,
    })

    var ydanyuanlou2 = "";
    this.setData({
      txt4: ydanyuanlou2,
    })

    
   }





    console.log("app.owner.ynumber" + app.owner.ynumber);
    var that = this;
    that.setData({ ynumber: app.owner.ynumber});
    that.setData({ yfilename: app.owner.yfilename});
    that.setData({ yname: app.owner.yname });
    that.setData({ yidcar: app.owner.yidcar });
    that.setData({ ydetailed: app.owner.unitdoor });
    that.setData({ 'dialog5.hidden': true });

    console.log("app.owner.yowner" + app.owner.yowner);
    that.setData({ yownerinit: app.owner.yowner});
    if (this.data.yownerinit == "") {
      that.setData({ yownerinit: 0 });
    } else {
      that.setData({ yownerinit: app.owner.yowner });
    }

    that.setData({ ysexinit: app.owner.ysex});
    if (this.data.ysexinit == "") {
      that.setData({ ysexinit: 0 });
    } else {
      that.setData({ ysexinit: app.owner.ysex});
    }
    that.setData({ name: app.owner.address });

    if (this.data.name==""){
      that.setData({ name: "小区位置"});
    }else{
      that.setData({ name: app.owner.address });
    }

    if (this.data.yidcar == "") {
      that.setData({ yidcar: "" });
    } else {
      that.setData({ yidcar: app.owner.yidcar });
    }
    
   /* wx.request({
      url: 'https://wwl.ycws.cc/ssm/owner/selectByid',
      data: {
        ynumber: app.owner.ynumber,
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        that.setData({ yphones: res.data.yphone });
        console.log(res.data);
        
      }
    })*/
  },
  //身份证

  //性别
  bindPickerChange: function (e) {
    this.setData({
      ysexinit: e.detail.value
    })
    console.log("ysexinit" + this.data.ysexinit);
  },
  //业主
  bindPickerChange1: function (e) {
    this.setData({
      yownerinit: e.detail.value
    })
    console.log("yownerinit" + this.data.yownerinit);
  },
  uploadChange: function (e) {
    this.setData({
      upload: {
        hidden: true
      }
    });
  },
  //提示语句
  toast3Change: function (e) {
    this.setData({
      dialog3: {
        hidden: true
      }
    });
  },
  //提示语句
  ynameChange: function (e) {
    this.setData({
      ynameHidden: {
        hidden: true
      }
    });
  },
  //提示语句
  yphoneChange: function (e) {
    this.setData({
      yphoneHidden: {
        hidden: true
      }
    });
  },
  //提示语句
  yfilenameChange: function (e) {
    this.setData({
      yfilenameHidden: {
        hidden: true
      }
    });
  },
  //提示语句
  ydetailedChange: function (e) {
    this.setData({
      ydetailedHidden: { hidden: true }
    });
  },
  //提示语句
  yaddressChange: function (e) {
    this.setData({
      yaddressHidden: {
        hidden: true
      }
    });
  },
  //提示语句
  imageChange: function (e) {
    this.setData({
      imageHidden: {
        hidden: true
      }
    });
  },
  //提示语句
  systemChange: function (e) {
    this.setData({
      systemHidden: {
        hidden: true
      }
    });
  },
  //提示语句
  longlatitudeChange: function (e) {
    this.setData({
      longlatitudeHidden: {
        hidden: true
      }
    });
  },
  //提示语句
  fangjianhao: function (e) {
    this.setData({
      yfangjianhao: {
        hidden: true
      }
    });
  },
  //提示语句
  yfangjianhaom: function (e) {
    this.setData({
      yfangjianhao2: {
        hidden: true
      }
    });
  }, 
  //图片选择1
  uploads: function () {
    var that = this;
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['compressed'],
      //sizeType: ['original', 'compressed'],   // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'],         // 可以指定来源是相册还是相机，默认二者都有
      success: function (res1) {
  
        that.setData({ 'dialog4.hidden': false });
        console.log(res1.tempFilePaths[0])
        wx.uploadFile({
          url: 'https://wwl.ycws.cc/ssm/face/image',
          //url: 'http://localhost:9090/ssm/face/image',
          filePath: res1.tempFilePaths[0],
          name: 'filename',
          success: function (res) {
            console.log(res);
            console.log(res.data);
            var s = JSON.parse(res.data);
            var mes = s.error_message;
            if (mes == "CONCURRENCY_LIMIT_EXCEEDED") {
              console.log("图片上传有误，请重试");
              that.setData({ 'systemHidden.hidden': false });
              that.setData({ 'dialog4.hidden': true })
            } if (s.faces.length == 0) {
              console.log("不是人脸图片");
              that.setData({ 'imageHidden.hidden': false });
              that.setData({ 'dialog4.hidden': true })
            } else if (s.faces.length >= 1) {
              
              console.log("是人脸图片");
              that.setData({ 'dialog4.hidden': true })
              that.setData({
                yfilename: res1.tempFilePaths[0],
              })
            }
          }
        })
      }
    })
  },
  /*//图片选择2
  uploads1: function () {
    var that = this;
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['compressed'],
      //sizeType: ['original', 'compressed'],    // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'],          // 可以指定来源是相册还是相机，默认二者都有
      success: function (res1) {
        wx.uploadFile({
          url: 'https://wwl.ycws.cc/ssm/face/image',
          filePath: res1.tempFilePaths[0],
          name: 'filename',
          success: function (res) {
            var s = JSON.parse(res.data);
            var mes = s.error_message;
            if (mes == "CONCURRENCY_LIMIT_EXCEEDED") {
              console.log("系统上传有误，请重试");
              that.setData({ 'systemHidden.hidden': false });
            }
            console.log(s.faces.length);
            if (s.faces.length == 0) {
              console.log("不是人脸图片");
              that.setData({ 'imageHidden.hidden': false });
            } else if (s.faces.length >= 1) {
              console.log("是人脸图片");
              that.setData({
                yfilename1: res1.tempFilePaths[0],
              })
            }
          }
        })
      }
    })
  },
  //图片选择3
  uploads2: function () {
    var that = this;
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['compressed'],
      //sizeType: ['original', 'compressed'],    // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'],          // 可以指定来源是相册还是相机，默认二者都有
      success: function (res1) {
        wx.uploadFile({
          url: 'https://wwl.ycws.cc/ssm/face/image',
          filePath: res1.tempFilePaths[0],
          name: 'filename',
          success: function (res) {
            var s = JSON.parse(res.data);
            var mes = s.error_message;
            if (mes == "CONCURRENCY_LIMIT_EXCEEDED") {
              console.log("系统上传有误，请重试");
              that.setData({ 'systemHidden.hidden': false });
            }
            if (s.faces.length == 0) {
              console.log("不是人脸图片");
              that.setData({ 'imageHidden.hidden': false });
            } else if (s.faces.length >= 1) {
              console.log("是人脸图片");
              that.setData({
                yfilename2: res1.tempFilePaths[0],
              })
            }
          }
        })
      }
    })
  },*/
  primary: function (e) {
   
    var that = this
    var yfilename = this.data.yfilename;
   /* var yfilename1 = this.data.yfilename1;
    var yfilename2 = this.data.yfilename2;*/
    var ynames = e.detail.value["yname"]
    var yphones = e.detail.value["yphones"]
    var ydetaileds = e.detail.value["ydetailed"]
    var yindexs = e.detail.value["yindex"]
    var createtime = e.detail.value["createtime"]
    var ynumbers = e.detail.value["ynumber"]
    var yidcar = e.detail.value["yidcar"]
    var ydanyuanlou = e.detail.value["ydanyuanlou"]
    var yfanghao = e.detail.value["yfanghao"]
    var ysexs = this.data.ysexinit;
    if (ysexs == 0) {
      var ysexs = "男";
    } else if (ysexs == 1) {
      var ysexs = "女";
    }

    var yowners = this.data.yownerinit;
    if (yowners == 0) {
      var yowners = "业主";
    } else if (yowners == 1) {
      var yowners = "家庭";
    } else if (yowners == 2) {
      var yowners = "亲戚";
    } else if (yowners == 3) {
      var yowners = "朋友";
    } else if (yowners == 4) {
      var yowners = "其他";
    }
    var name = /(?!(\D*\d){11})^[\u4e00-\u9fa5\w]{2,21}$/;
    var yaddresss = that.data.name;

    var fanghaoyz =/^\d{2,6}$/;

    if (yfilename == "") {
      that.setData({ 'yfilenameHidden.hidden': false });
    } else if (!name.test(ynames)){
      that.setData({ 'ynameHidden.hidden': false });
    }else if (ynames == "") {
      that.setData({ 'ynameHidden.hidden': false });
    } else if (yphones == "") {
      that.setData({ 'yphoneHidden.hidden': false });
    } else if (yaddresss == "小区位置") {
      that.setData({ 'yaddressHidden.hidden': false });
    } else if (ydetaileds == "") {
      that.setData({ 'ydetailedHidden.hidden': false });
    } else if (yfanghao == ""){
      that.setData({ 'yfangjianhao.hidden': false });
    } else if (!fanghaoyz.test(yfanghao)) {
      that.setData({ 'yfangjianhao2.hidden': false });
    }else {
      that.setData({ 'dialog3.hidden': false });
      that.setData({
        formSubmit: "nosubmit",
      });
      var arrName = [];
      arrName.push("file_pic1");

      var arrURL = [];
      arrURL.push(yfilename);

      var b = 0;
      
      uploadFile(arrName, arrURL, b, ynames, ysexs, yowners, yphones, yaddresss, ynumbers, ydetaileds, yidcar, ydanyuanlou, yfanghao)
    }
  },
  address: function (e) {
    var phonestwo=e.detail.value.yphones 
    wx.setStorageSync("phonetwo", phonestwo);
    var phoness = e.detail.value.yphones
    var yidcar = e.detail.value["yidcar"]
    wx.setStorageSync("yidcar", yidcar);
    var ydanyuanlou = e.detail.value.ydanyuanlou
 
    var yfanghao = e.detail.value.yfanghao
    wx.setStorageSync("yfanghao", yfanghao);
    wx.setStorageSync("ydanyuanlou", ydanyuanlou);
    var that = this;
    var a = this.data.yfilename;
    var d = e.detail.value["yname"]
    var e = this.data.yownerinit;
    var f = this.data.ysexinit;
    //that.setData({ 'dialog5.hidden': false });
    wx.chooseLocation({
      type: 'wgs84',
      success: function (res) {
        
        var latitude = res.latitude
        var longitude = res.longitude
        var speed = res.speed
        var accuracy = res.accuracy
        var name = res.name
        console.log(longitude);
        console.log(latitude);
        console.log(res.address);
        that.setData({ 'dialog5.hidden': true });
        wx.request({
          url: 'https://wwl.ycws.cc/ssm/location/longlatitude',
          data: {
            longitude: longitude,
            latitude: latitude
          },
          header: {
            'content-type': 'application/json'
          },
          success: function (res) {
          
            console.log(res.data.location);
            if (res.data.location == undefined) {
              that.setData({ 'longlatitudeHidden.hidden': false });
            } else {

              var an = res.data;
              var json = JSON.stringify(an);
              console.log("an" + json);
              wx.redirectTo({
                url: '../../pages/showaddress/showaddress?a=' + json + '&b=' + that.data.ynumber + '&aa=' + a + '&dd=' + d + '&ee=' + e + '&ff=' + f
              })
            }
          }
        })
      },
    })
  },
})
function uploadFile(arrName, arrURL, b, ynames, ysexs, yowners, yphones, yaddresss, ynumbers, ydetaileds, yidcar, ydanyuanlou, yfanghao)
{
  
  var that = this
  try {
    
    var filePath = arrURL[b].toString();
    var filename = arrName[b];
    var _url = 'https://wwl.ycws.cc/ssm/uploadfile/addfile2';
    //var _url = 'http://localhost:9090/ssm/uploadfile/addfile2';
  } catch (e) { }
  if (filePath) {
    wx.uploadFile({
      url: _url,
      filePath: filePath, //这是文件url
      name: 'filename',   //这是表示当前文件的name
      header: { "Content-Type": "multipart/form-data" },
      formData: {
        filePath: filePath,
        name: filename,
        'yname': encodeURI(ynames),
        'ysex': encodeURI(ysexs),
        'yowner': encodeURI(yowners),
        'yphone': encodeURI(yphones),
        'yaddress': encodeURI(yaddresss),
        //'ynumber': encodeURI(ynumbers),
        'ynumber': wx.getStorageSync("ynumbertwo"),
        'ydetailed': encodeURI(ydetaileds),
        'yidcar': encodeURI(yidcar),
        'ydanyuanlou': encodeURI(ydanyuanlou),
        'yfanghao': encodeURI(yfanghao),
      },
      success: function (res) {
       
        var s = JSON.parse(res.data);
        if (s.code == '00') {
         /** b++;
          console.log("第" + b + "张图片，上传完成")
          uploadFile(arrName, arrURL, b, ynames, ysexs, yowners, yphones, yaddresss, ynumbers, ydetaileds)*/
          console.log("----------图片上传完成-------------");
          wx.redirectTo({
            url: '../../pages/newshowuser/newshowuser?ynumber=' + wx.getStorageSync("ynumbertwo")//直接取值
          })
        } else {
          util.msg("错误", "网络异常");
          return;
        }
      },
      fail: function (res) {
        util.msg("错误", "网络异常");
        return;
      }
    })
  } else {
    console.log("----------图片上传完成-------------");
    wx.redirectTo({
      url: '../../pages/newshowuser/newshowuser?ynumber=' + ynumbers
    })
  }
}




